<?php
declare(strict_types=1);
/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://gitee.com/xmo/MineAdmin
 */

namespace Plugin\Device\Service;

use App\Exception\BusinessException;
use App\Http\Common\ResultCode;
use Plugin\Device\Repository\RdDeviceBroadcastRepository;
use App\Service\IService;
use Plugin\Device\Repository\RdDeviceRodRepository;

/**
 * 广播设备服务类
 */
final class RdDeviceBroadcastService extends IService
{
    public function __construct(
        protected readonly RdDeviceBroadcastRepository $repository,
        protected readonly RdDeviceRodRepository $rodRepository,
    ) {}

    public function create(array $data): mixed
    {
        $rodId = $data['rod_id'];
        $rod = $this->rodRepository->findById($rodId);
        if (!$rod) {
            throw new BusinessException(ResultCode::NOT_FOUND, '装置不存在');
        }
        $data['stand_id'] = $rod->stand_id;
        return parent::create($data);
    }
}